package com.xsmart.oa.dao;

import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author chen
 */
@Component
public interface MemberDAO {

    /**
     * 会员列表查询
     *
     * @param page
     * @param nickName 会员名
     * @return
     *
     *2018/3/5 修改内容：将表连接改为左连接，修改如果无会员等级则该字段为'无'，新增查询分销商真实姓名字段
     * 修改者：xiaofeng
     */
    @Select("<script>select i.user_id, " +
            " (case when user_type='0' then '普通会员' when user_type='1' then '分销会员' end) " +
            " user_type,i.nick_name,i.password,i.question,i.answer,i.user_money, " +
            "i.frozen_money,i.pay_points,i.rank_points,i.visit_count,i.user_rank, "+
            "(case when c.rank_name is null then '无' else c.rank_name end)rank_name, " +
            "(case when is_validated='0' then '不生效' when is_validated = '1' then '生效' end)is_validated, "+
            "(case when t.user_name is null then '平台会员' when t.status='0' then '平台会员' else t.user_name end)user_name "+
            "from INFS_USER i "+
            " left join infu_user_request_type_task t on t.user_id=i.user_id "+
            " left join CFGU_user_rank c on i.user_rank=c.rank_id" +
            "<where> " +
            "<if test=\"nickName==null\">i.nick_name like concat('%','%')</if> "+
            "<if test=\"nickName!=null\">i.nick_name like concat('%',#{nickName},'%') "+
            "</if>" +
            "</where>" +
            "</script>")
    List<Map<String, Object>> queryUserPage(Pagination page, @Param("nickName") String nickName);

    /**
     * 查询会员等级
     *
     * @return
     */
    @Select("select rank_name from CFGU_user_rank order by rank_id")
    List<Map<String, Object>> queryRankNames();

    /**
     * 删除会员
     *
     * @param userId 会员id
     * @return
     */
    @Delete("delete from INFS_USER where user_id=#{userId}")
    int deleteUserById(@Param("userId") Integer userId);

    /**
     * 修改会员信息
     *
     * @param userMap
     * @return
     */
    @Update("update INFS_USER set " +
            "user_type=#{userMap.userType}," +
            "email=#{userMap.email}," +
            "nick_name=#{userMap.nickName}," +
            "password=#{userMap.password}," +
            "question=#{userMap.question}," +
            "answer=#{userMap.answer}," +
            "sex=#{userMap.sex}," +
            "birthday=#{userMap.birthday}," +
            "user_money=#{userMap.userMoney}," +
            "frozen_money=#{userMap.frozenMoney}," +
            "pay_points=#{userMap.payPoints}," +
            "rank_points=#{userMap.rankPoints}," +
            "address_id=#{userMap.addressId}," +
            "reg_time=#{userMap.regTime}," +
            "last_login=#{userMap.lastLogin}," +
            "last_mod_time=#{userMap.lastModTime}," +
            "last_ip=#{userMap.lastIp}," +
            "visit_count=#{userMap.visitCount}," +
            "user_rank=#{userMap.userRank}," +
            "msn=#{userMap.msn}," +
            "qq=#{userMap.qq}," +
            "office_phone=#{userMap.officePhone}," +
            "home_phone=#{userMap.homePhone}," +
            "mobile_phone=#{userMap.mobilePhone}," +
            "is_validated=#{userMap.isValidated}," +
            "user_picture=#{userMap.userPicture} " +
            "where user_id=#{userMap.userId}")
    int updateUserById(@Param("userMap") Map<String, Object> userMap);

}
